package club.kingyin.easycache.component;

import java.util.concurrent.TimeUnit;

public class DefaultTimer implements Timer {

    private long time;

    private TimeUnit timeUnit;

    public DefaultTimer(long time, TimeUnit timeUnit) {
        this.timeUnit = timeUnit;
        this.time = time;
    }

    public long getTime() {
        return time;
    }

    public TimeUnit getTimeUnit() {
        return timeUnit;
    }

    public void setTimeUnit(TimeUnit timeUnit) {
        this.timeUnit = timeUnit;
    }

    public void setTime(long time) {
        this.time = time;
    }

    @Override
    public long millis() {
        return timeUnit.toMillis(time);
    }

    static class Builder {
        private long time;
        private TimeUnit timeUnit = TimeUnit.SECONDS;

        public Builder setTimeUnit(TimeUnit timeUnit) {
            this.timeUnit = timeUnit;
            return this;
        }

        public Builder setTime(long time) {
            this.time = time;
            return this;
        }

        public Timer build() {
            return new DefaultTimer(time,timeUnit);
        }
    }
}
